package com.octopus.crontab;

import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.TimerTask;

public class CrontabTask extends TimerTask {
	
	public CrontabTimeMatcher timeMatcher;
	protected HashMap<String, Object> inputParameters;
	private Map<String, Object> contextRef = null;

	public CrontabTask(String crontabString, HashMap<String, Object> inputParameters) throws CrontabTimerNotMatchException{
		timeMatcher = new CrontabTimeMatcher(crontabString);
		this.inputParameters = inputParameters;
	}
	
	public void setContext(Map<String, Object> context){
		if(context == null){
			this.contextRef = new Hashtable<String, Object>();
		}
		this.contextRef = context;
	}
	
	protected Map<String, Object> getContextRef(){
		return this.contextRef;
	}
	
	public void run() {
		long time = scheduledExecutionTime();
		Date date = new Date(time);
		if(timeMatcher.timeMatch(date)){
			try{
				runLogic();
			}catch(Exception e){}
		}
	}

	protected void runLogic(){
	}
}
